package com.jereh.topnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jereh.topnews.R;
import com.jereh.topnews.entity.Content;

import java.util.List;

/**
 * 新闻列表的适配器
 * Created by Administrator on 2016/11/3.
 */
public class NewsListAdapter extends BaseAdapter{

    private Context ctx;
    private List<Content> list;

    public static final int TYPE_PIC=0;
    public static final int TYPE_BIG_PIC=1;
    public static final int TYPE_NO_PIC=2;

    public NewsListAdapter(Context ctx, List<Content> list) {
        this.ctx = ctx;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).getImageurls()==null||list.get(position).getImageurls().isEmpty()){
            return TYPE_NO_PIC;
        } else if(list.get(position).getImageurls().size()<3){
            return TYPE_BIG_PIC;
        } else {
            return TYPE_PIC;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderPic holderPic=null;
        ViewHolderBigPic holderBig=null;
        ViewHolderNoPic holderNo=null;
        int layoutType=getItemViewType(position);
        //当convertView为空时，使用布局加载器去加载一个布局
        if(convertView==null) {
            switch (layoutType) {
                case TYPE_PIC:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.adapter_news_img,null);
                    //实例化viewholder
                    holderPic=new ViewHolderPic();
                    holderPic.title=(TextView)convertView.findViewById(R.id.title);
                    holderPic.img1=(ImageView)convertView.findViewById(R.id.img1);
                    holderPic.img2=(ImageView)convertView.findViewById(R.id.img2);
                    holderPic.img3=(ImageView)convertView.findViewById(R.id.img3);
                    holderPic.author=(TextView)convertView.findViewById(R.id.author);
                    holderPic.reply=(TextView)convertView.findViewById(R.id.reply);
                    holderPic.date=(TextView)convertView.findViewById(R.id.date);
                    convertView.setTag(R.id.newsImg,holderPic);
                    break;
                case TYPE_BIG_PIC:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.adapter_news_big_img,null);
                    //实例化viewholder
                    holderBig=new ViewHolderBigPic();
                    holderBig.title=(TextView)convertView.findViewById(R.id.title);
                    holderBig.bigImg=(ImageView)convertView.findViewById(R.id.bigImg);
                    holderBig.author=(TextView)convertView.findViewById(R.id.author);
                    holderBig.reply=(TextView)convertView.findViewById(R.id.reply);
                    holderBig.picCount=(TextView)convertView.findViewById(R.id.picCount);
                    convertView.setTag(R.id.newsBigImg,holderBig);
                    break;
                case TYPE_NO_PIC:
                    convertView= LayoutInflater.from(ctx).inflate(R.layout.adapter_news_nopic,null);
                    //实例化viewholder
                    holderNo=new ViewHolderNoPic();
                    holderNo.title=(TextView)convertView.findViewById(R.id.title);
                    holderNo.author=(TextView)convertView.findViewById(R.id.author);
                    holderNo.reply=(TextView)convertView.findViewById(R.id.reply);
                    holderNo.date=(TextView)convertView.findViewById(R.id.date);
                    convertView.setTag(R.id.newsNoImg,holderNo);
                    break;
            }

        } else {
            switch (layoutType) {
                case TYPE_PIC:
                    holderPic=(ViewHolderPic)convertView.getTag(R.id.newsImg);
                    break;
                case TYPE_BIG_PIC:
                    holderBig=(ViewHolderBigPic)convertView.getTag(R.id.newsBigImg);
                    break;
                case TYPE_NO_PIC:
                    holderNo=(ViewHolderNoPic)convertView.getTag(R.id.newsNoImg);
                    break;
            }
        }
        //进行赋值
        Content news=list.get(position);
        switch (layoutType) {
            case TYPE_PIC:
                holderPic.title.setText(news.getTitle());
                setImageBitmap(holderPic.img1,news.getImageurls().get(0).getUrl());
                setImageBitmap(holderPic.img2,news.getImageurls().get(1).getUrl());
                setImageBitmap(holderPic.img3,news.getImageurls().get(2).getUrl());
                holderPic.author.setText(news.getSource());
                holderPic.reply.setText(news.getReply() + "评论数");
                holderPic.date.setText(news.getPubDate());
                break;
            case TYPE_BIG_PIC:
                holderBig.title.setText(news.getTitle());
//                holderBig.bigImg.setImageResource(news.getImg1());
                setImageBitmap(holderBig.bigImg,news.getImageurls().get(0).getUrl());
                holderBig.author.setText(news.getSource());
                holderBig.reply.setText(news.getReply()+"评论数");
                holderBig.picCount.setText(news.getImageurls().size()+"图");
                break;
            case TYPE_NO_PIC:
                holderNo.title.setText(news.getTitle());
                holderNo.author.setText(news.getSource());
                holderNo.reply.setText(news.getReply()+"评论数");
                holderNo.date.setText(news.getPubDate());
                break;
        }
        return convertView;
    }

    /**
     * 处理图片的整个流程
     */
    public void setImageBitmap(final ImageView imageView,final String imageUrl){

//        Glide.with(ctx).load(imageUrl).into(imageView);
//        with:图片上下文
//        load:图片网址
//        into:放置图片的控件
//        placeholder:设置下载过程中的过渡图片
//        error:加载失败显示的图片
//        skipMemoryCache:设置跳过内存缓存
//        priority:设置项在图片的优先级
//        diskCacheStrategy:设置缓存策略
//        all:缓存源资源和转换后的资源
//
//        none:不作任何磁盘缓存
//
//        source:缓存源资源
//
//        result：缓存转换后的资源
//        animate:设置加载动画
        Glide.with(ctx).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.xh_ic_loading).error(R.mipmap.not_network_loading).into(imageView);
    }

    /**
     * 存储控件
     */
    static class ViewHolderPic{
        TextView title;//标题
        ImageView img1;//图片一
        ImageView img2;//图片二
        ImageView img3;//图片三
        TextView author;//作者
        TextView reply;//评论数
        TextView date;//时间
    }
    static class ViewHolderBigPic{
        TextView title;//标题
        ImageView bigImg;//图片
        TextView author;//作者
        TextView reply;//评论数
        TextView picCount;//图片数
    }
    static class ViewHolderNoPic{
        TextView title;//标题
        TextView author;//作者
        TextView reply;//评论数
        TextView date;//时间
    }
}
